package com.zhuss.appconfig;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zhuss.util.RedisRule;
/**
 * token 工具类
 * @author zhushanshuo
 * @date   2020.01.16 13:01
 */
@Component
public final class TokenProcessor {
	
	@Autowired
	private RedisProcessor redisProcessor;
	
	public Long getUserId(String token) {
		Object userIdObj = redisProcessor.get(RedisRule.genLoginTokenKey(token));
		Long userId = null;
		if (userIdObj != null) {
			userId = ((Integer)userIdObj).longValue();
		}
		return userId;
	}
	
	public void setUserId(String token, Long userId) {
		redisProcessor.set(RedisRule.genLoginTokenKey(token), userId, 60*60*8);
		redisProcessor.set(RedisRule.genLoginUserKey(userId), token, 60*60*8);
	}
	
	public void setUserIdForever(String token, Long userId) {
		redisProcessor.set(RedisRule.genLoginTokenKey(token), userId);
		redisProcessor.set(RedisRule.genLoginUserKey(userId), token);
	}
	
	public void delUserId(String token, Long userId) {
		redisProcessor.del(RedisRule.genLoginTokenKey(token));
    	redisProcessor.del(RedisRule.genLoginUserKey(userId));
	}
	
}
